package com.yuexun.dhdoctor.module.mythings.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.baselistener.OnDataBackListener;
import com.yuexun.dhdoctor.module.mythings.ui.fragment.Frag_Agent;
import com.yuexun.dhdoctor.module.mythings.ui.fragment.Frag_All;
import com.yuexun.dhdoctor.module.mythings.ui.fragment.Frag_CopyMe;
import com.yuexun.dhdoctor.module.mythings.ui.fragment.Frag_Do;
import com.yuexun.dhdoctor.module.mythings.ui.fragment.Frag_StartMy;
import com.yuexun.dhdoctor.util.DateUtil;
import com.yuexun.dhdoctor.widget.pickerUtil.CustomDatePicker;

import org.kymjs.kjframe.ui.KJFragment;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2017/2/27.
 */
public class Act_Mythings_New extends BaseKJActivity {
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.things_agent_tv)
    TextView thingsAgentTv;
    @Bind(R.id.things_agent_img)
    ImageView thingsAgentImg;
    @Bind(R.id.things_do_tv)
    TextView thingsDoTv;
    @Bind(R.id.things_do_img)
    ImageView thingsDoImg;
    @Bind(R.id.things_my_tv)
    TextView thingsMyTv;
    @Bind(R.id.things_my_img)
    ImageView thingsMyImg;
    @Bind(R.id.things_all_tv)
    TextView thingsAllTv;
    @Bind(R.id.things_all_img)
    ImageView thingsAllImg;
    @Bind(R.id.etSearch2)
    EditText etSearch2;
    @Bind(R.id.searchLayout)
    LinearLayout searchLayout;
    @Bind(R.id.btnSearchClear)
    ImageButton btnSearchClear;
    //抄送我的
    @Bind(R.id.things_copy_me_tv)
    TextView thingsCopyMeTv;
    @Bind(R.id.things_copy_me_img)
    ImageView thingsCopyMeImg;
    @Bind(R.id.things_copy_me_ll)
    RelativeLayout thingsCopyMeLl;
    @Bind(R.id.tv_date_from)
    TextView tvDateFrom;
    @Bind(R.id.tv_date_to)
    TextView tvDateTo;
    @Bind(R.id.ll_select_time)
    LinearLayout llSelectTime;


    KJFragment mContentFragment;

    Frag_Agent fragment_agent;
    Frag_Do fragment_do;
    Frag_StartMy fragment_my;
    Frag_All fragment_all;
    Frag_CopyMe frag_copyMe;

    OnDataBackListener mCallBack;


    public void setOnSearchCallBack(OnDataBackListener callBack) {
        this.mCallBack = callBack;
        refreshKey("1");
    }

    private void refreshKey(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("key", etSearch2.getText().toString());
        bundle.putString("startDate", tvDateFrom.getText().toString());
        bundle.putString("endDate", tvDateTo.getText().toString());
        bundle.putString("type", type);
        mCallBack.showData(bundle);
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.act_things_new);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        etSearch2.setText("");
    }

    @Override
    public void initData() {
        super.initData();
        titleTv.setText("事项审批");
        llSelectTime.setVisibility(View.VISIBLE);
        tvDateTo.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        tvDateFrom.setText(new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getDateBefore(new Date(), 30)));
        fragment_agent = new Frag_Agent();
        fragment_do = new Frag_Do();
        fragment_my = new Frag_StartMy();
        fragment_all = new Frag_All();
        frag_copyMe = new Frag_CopyMe();

        mContentFragment = fragment_agent;
        setOnSearchCallBack((OnDataBackListener) mContentFragment);
        changeFragment(R.id.things_content, mContentFragment);

        etSearch2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                    // search pressed and perform your functionality.
                    refreshKey("2");
                }
                return false;
            }
        });

        etSearch2.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
                if (etSearch2.getText().toString().length() == 0) {
                    searchLayout.setVisibility(View.VISIBLE);
                    btnSearchClear.setVisibility(View.INVISIBLE);

                } else {
                    searchLayout.setVisibility(View.GONE);
                    btnSearchClear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
        });
//        thingsCopyMeLl.setVisibility(View.VISIBLE);
    }

    @Override
    @OnClick({R.id.back, R.id.things_add, R.id.things_agent_ll, R.id.things_do_ll, R.id.ll_date_from, R.id.ll_date_to,
            R.id.things_my_ll, R.id.things_all_ll, R.id.things_copy_me_ll, R.id.btnSearchClear})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.ll_date_from:
                CustomDatePicker customDatePicker = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        // 回调接口，获得选中的时间
                        tvDateFrom.setText(time);
                        refreshKey("2");
                    }
                }, "2010-01-01 00:00", tvDateFrom.getText().toString() + " 00:00"); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker.showSpecificTime(false); // 显示时和分
                customDatePicker.setIsLoop(true); // 不允许循环滚动
                customDatePicker.show(tvDateFrom.getText().toString() + " 00:00");
                break;
            case R.id.ll_date_to:
                CustomDatePicker customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        // 回调接口，获得选中的时间
                        tvDateTo.setText(time);
                        refreshKey("2");
                    }
                }, "2010-01-01 00:00", tvDateTo.getText().toString() + " 00:00"); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker1.showSpecificTime(false); // 显示时和分
                customDatePicker1.setIsLoop(true); // 不允许循环滚动
                customDatePicker1.show(tvDateTo.getText().toString() + " 00:00");
                break;
            case R.id.things_add:
                Intent intent = new Intent(this, Act_Thing_Add.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", "1");
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.things_agent_ll:
                thingsAgentTv.setTextColor(getResources().getColor(R.color.doc_advice_01));
                thingsAgentImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                thingsDoTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsDoImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsMyTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsMyImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsAllTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsAllImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsCopyMeTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsCopyMeImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));

                mContentFragment = fragment_agent;
                setOnSearchCallBack((OnDataBackListener) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                break;
            case R.id.things_do_ll:
                thingsAgentTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsAgentImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsDoTv.setTextColor(getResources().getColor(R.color.doc_advice_01));
                thingsDoImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                thingsMyTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsMyImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsAllTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsAllImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsCopyMeTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsCopyMeImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));

                mContentFragment = fragment_do;
                setOnSearchCallBack((OnDataBackListener) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                break;
            case R.id.things_my_ll:
                thingsAgentTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsAgentImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsDoTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsDoImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsMyTv.setTextColor(getResources().getColor(R.color.doc_advice_01));
                thingsMyImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                thingsAllTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsAllImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsCopyMeTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsCopyMeImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));

                mContentFragment = fragment_my;
                setOnSearchCallBack((OnDataBackListener) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                break;
            case R.id.things_all_ll:
                thingsAgentTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsAgentImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsDoTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsDoImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsMyTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsMyImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsAllTv.setTextColor(getResources().getColor(R.color.doc_advice_01));
                thingsAllImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                thingsCopyMeTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsCopyMeImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));

                mContentFragment = fragment_all;
                setOnSearchCallBack((OnDataBackListener) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                break;
            case R.id.btnSearchClear:
                etSearch2.setText("");
                break;
            case R.id.things_copy_me_ll:
                thingsAgentTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsAgentImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsDoTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsDoImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsMyTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsMyImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsAllTv.setTextColor(getResources().getColor(R.color.doc_advice_03));
                thingsAllImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                thingsCopyMeTv.setTextColor(getResources().getColor(R.color.doc_advice_01));
                thingsCopyMeImg.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));

                mContentFragment = frag_copyMe;
                setOnSearchCallBack((OnDataBackListener) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                break;

        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
